package com.tzpay.tzpay.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.rey.material.widget.CheckBox;
import com.tzpay.tzpay.R;
import com.tzpay.tzpay.presenter.LoginLogicView;
import com.tzpay.tzpay.presenter.LoginPresenter;
import com.tzpay.tzpay.ui.BaseActivity;
import com.umeng.update.UmengUpdateAgent;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by chenfeiyue on 16/4/7.
 */
public class LoginActivity extends BaseActivity implements LoginLogicView, LoginLogicView.LoginListener {

    @Bind(R.id.username)
    EditText tvUserName;
    @Bind(R.id.password)
    EditText tvPassWord;
    @Bind(R.id.button)
    TextView button;
    @Bind(R.id.cb_remenber_passwd)
    CheckBox cbRemenberPasswd;
    @Bind(R.id.progress)
    ProgressBar progress;

    private LoginPresenter presenter;

    @Override
    public void setupContentView() {
        setContentView(R.layout.activity_login);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasAppBar(false);
        UmengUpdateAgent.setDefault();
        UmengUpdateAgent.update(getApplication());

        presenter = new LoginPresenter(LoginActivity.this);
        presenter.setLoginListener(LoginActivity.this);
        presenter.init();
    }

    @OnClick(R.id.button)
    public void onLoginClick() {
        presenter.login(tvUserName.getText().toString(), tvPassWord.getText().toString(), cbRemenberPasswd.isChecked());
    }

    @Override
    public void goMainActivity() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
        overridePendingTransition(R.anim.activity_animation_right_left,
                R.anim.activity_animation_right_left_exit);
    }

    @Override
    public void finishActivity() {
        finish();
    }

    @Override
    public void renderLasLoginUserInfo(String username, String password, boolean isRememberPass) {
        this.tvUserName.setText(username);
        this.tvPassWord.setText(password);
        this.cbRemenberPasswd.setChecked(isRememberPass);
    }

    @Override
    public void setUsernameError() {
        hideLoading();
        tvUserName.requestFocus();
        tvUserName.setError(getString(R.string.username_error));
        tvPassWord.setError(null);
    }

    private void hideLoading() {
        button.setEnabled(true);
        progress.setVisibility(View.GONE);
    }

    @Override
    public void setPasswordError() {
        hideLoading();
        tvPassWord.requestFocus();
        tvPassWord.setError(getString(R.string.password_error));
        tvUserName.setError(null);
    }

    @Override
    public void showProgress() {
        button.setEnabled(false);
        progress.setVisibility(View.VISIBLE);
    }

    @Override
    public void onFailed(String resperr) {
        hideLoading();
        // 登录失败
        hideLoading();
        if (TextUtils.isEmpty(resperr)) {
            resperr = getString(R.string.text_login_fail);
        }
        showToast(resperr);
    }

    @Override
    public void onSuccess(Object result) {
        hideLoading();
    }
}
